package com.zzsy.carosshopprojects.activitys.home.mainpage.common.payresult;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.MessageEvent;
import com.zzsy.carosshopprojects.view.TitleControlView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 支付失败 状态异常
 */
public class PayFailFragment extends BaseFragment {
    public static PayFailFragment newInstance(String code, String payAmount) {
        PayFailFragment payFailFragment = new PayFailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("code", code);
        bundle.putString("payAmount", payAmount);
        payFailFragment.setArguments(bundle);
        return payFailFragment;
    }

    @BindView(R.id.tv_print_two)
    TextView tvPrintTwo;
    @BindView(R.id.tv_order_money)
    TextView tvOriderMoney;
    @BindView(R.id.tv_state)
    TextView tv_state;
    @BindView(R.id.iv_state)
    ImageView iv_state;
    private String title;
    private int img;
    private TitleControlView t;

    @Override
    protected int initLayout() {
        return R.layout.fg_payfail;
    }

    @Override
    protected void initView(View view) {
        if (getArguments().getString("code").equals("502")) {
            title = getResources().getString(R.string.payFail);
            img = R.mipmap.icon_fail;
        } else {
            title = getResources().getString(R.string.payLoad);
            img = R.mipmap.icon_dai;
        }
        tv_state.setText(title);
        initTitle(view, title, getActivity());
        t = new TitleControlView(view, () -> goBack(getActivity()));
        t.setTitle(title);
        iv_state.setImageResource(img);
      //  tvPrintTwo.setBackground(ViewBgUtil.setBg(360, Color.parseColor(ConfigureFile.ColorSystem)));
        tvOriderMoney.setText(getArguments().getString("payAmount") + "元");
    }

    @Override
    protected void initData(Context mContext) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void loadMethod() {

    }

    @OnClick({R.id.tv_print_two})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_print_two:
                getActivity().finish();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {//接收消息推送的回调
        switch (event.message) {
            case MessageEvent.PAYSUCESS:
                if (t != null) {
                    t.setTitle(getResources().getString(R.string.paySuccess));
                    iv_state.setImageResource(R.mipmap.icon_success);
                    tv_state.setText(getResources().getString(R.string.paySuccess));
                }
                break;
            case MessageEvent.RECHARGESUCESS:
                if (t != null) {
                    t.setTitle(getResources().getString(R.string.RechargeSuccess));
                    iv_state.setImageResource(R.mipmap.icon_success);
                    tv_state.setText(getResources().getString(R.string.RechargeSuccess));
                }
                break;
        }
    }

}
